package com.br4mmie.asciistory.graphics;

import android.content.*;
import android.content.res.*;
import android.graphics.*;
import android.graphics.drawable.*;

public class TextureLoader 
{
	private Context context;
	private Resources res;
	private int width;
	private int height;
	
	public TextureLoader(Context context)
	{
		setContext(context);
		width = 0;
		height = 0;
	}
	public void setContext(Context context)
	{
		this.context = context;
	}
	public byte[] loadTexture(int resId)
	{
		byte[] texture;
		Bitmap bitmap = ((BitmapDrawable)res.getDrawable(resId)).getBitmap();
		
		texture = new byte[bitmap.getWidth() * bitmap.getHeight() * 4];
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		
		int[] pixels = new int[width * height];
		bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
		
		for (int y=0; y<bitmap.getHeight(); ++y)
		{
			for (int x=0; x<bitmap.getWidth(); ++x)
			{
				int i = (x + y * bitmap.getWidth()) * 4;
				int pixel = pixels[x+y*width];
				
				texture[i + 0] = (byte) ((pixel >> 16) & 0xff);
				texture[i + 1] = (byte) ((pixel >>  8) & 0xff);
				texture[i + 2] = (byte) ((pixel >>  0) & 0xff);
				texture[i + 3] = (byte) ((pixel >> 24) & 0xff);
				
				if (pixel == 0xffff00ff) texture[i+3] = 0x00;
			}
		}
		
		return texture;
	}
	public int getWidth()
	{
		return width;
	}
	public int getHeight()
	{
		return height;
	}
}
